package com.dotstone.chipism.esptouch;

/*************************************************************************
 * 公       司： 深圳市高金科技有限公司
 * 作       者： 深圳市高金科技有限公司
 * 文件名称：DataCode.java
 * 内容摘要：
 * 日        期： 2016-11-28
 ************************************************************************/
public class DataCode implements ICodeData {

	public static final int DATA_CODE_LEN = 6;

	private static final int INDEX_MAX = 127;

	private final byte mSeqHeader;
	private final byte mDataHigh;
	private final byte mDataLow;
	// the crc here means the crc of the data and sequence header be transformed
	// it is calculated by index and data to be transformed
	private final byte mCrcHigh;
	private final byte mCrcLow;

	/**
	 * Constructor of DataCode
	 * @param u8 the character to be transformed
	 * @param index the index of the char
	 */
	public DataCode(char u8, int index) {
		if (index > INDEX_MAX) {
			throw new RuntimeException("index > INDEX_MAX");
		}
		byte[] dataBytes = ByteUtil.splitUint8To2bytes(u8);
		mDataHigh = dataBytes[0];
		mDataLow = dataBytes[1];
		CRC8 crc8 = new CRC8();
		crc8.update(ByteUtil.convertUint8toByte(u8));
		crc8.update(index);
		byte[] crcBytes = ByteUtil.splitUint8To2bytes((char) crc8.getValue());
		mCrcHigh = crcBytes[0];
		mCrcLow = crcBytes[1];
		mSeqHeader = (byte) index;
	}

	@Override
	public byte[] getBytes() {
		byte[] dataBytes = new byte[DATA_CODE_LEN];
		dataBytes[0] = 0x00;
		dataBytes[1] = ByteUtil.combine2bytesToOne(mCrcHigh, mDataHigh);
		dataBytes[2] = 0x01;
		dataBytes[3] = mSeqHeader;
		dataBytes[4] = 0x00;
		dataBytes[5] = ByteUtil.combine2bytesToOne(mCrcLow, mDataLow);
		return dataBytes;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		byte[] dataBytes = getBytes();
		for (int i = 0; i < DATA_CODE_LEN; i++) {
			String hexString = ByteUtil.convertByte2HexString(dataBytes[i]);
			sb.append("0x");
			if (hexString.length() == 1) {
				sb.append("0");
			}
			sb.append(hexString).append(" ");
		}
		return sb.toString();
	}

	@Override
	public char[] getU8s() {
		throw new RuntimeException("DataCode don't support getU8s()");
	}

}
